package com.owen.server.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.owen.dao.StudentMapper;
import com.owen.pojo.Student;
import com.owen.server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServerImpl implements StudentServer {

    @Autowired(required = false)
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // studentMapper.queryAll()
    // 使用同步锁解决高并发问题
    // synchronized锁助方法可以有效解决高并发问题，但是会使得程序效率大大降低
//    @Override
//    public synchronized List<Student> queryAll() {
//        List<Student> studentList = null;
//
//        String student_list = (String)redisTemplate.opsForValue().get("Student_List");
//
//        // 判断redis中是否存在该值
//        if(student_list == null || student_list.isEmpty()){
//            System.out.println("-----------------在数据库中查看--------------------------");
//            // redis中没有该值
//            studentList = studentMapper.queryAll();
//            // 将该值存入redis
//            String jsonString = JSON.toJSONString(studentList);
//            redisTemplate.opsForValue().set("Student_List",jsonString);
//        }else{
//            System.out.println("-----------------在redis中库中查看--------------------------");
//            studentList = JSONArray.parseArray(student_list, Student.class);
//        }
//
//        return studentList;
//    }

    // 使用该方式可以高效率解决并发问题
    // 但是只能针对与同一个jvm中，
//    @Override
//    public List<Student> queryAll() {
//        List<Student> studentList = null;
//        String student_list = (String)redisTemplate.opsForValue().get("Student_List");
//        // 判断redis中是否存在该值
//        if(student_list == null || student_list.isEmpty()){
//           synchronized (this){
//               student_list = (String)redisTemplate.opsForValue().get("Student_List");
//               if(student_list == null || student_list.isEmpty()){
//                   System.out.println("-----------------在数据库中查看--------------------------");
//                   // redis中没有该值
//                   studentList = studentMapper.queryAll();
//                   // 将该值存入redis
//                   String jsonString = JSON.toJSONString(studentList);
//                   redisTemplate.opsForValue().set("Student_List",jsonString);
//               }else{
//                   System.out.println("-----------------在redis中库中查看--------------------------");
//                   studentList = JSONArray.parseArray(student_list, Student.class);
//               }
//           }
//        }else{
//            System.out.println("-----------------在redis中库中查看--------------------------");
//            studentList = JSONArray.parseArray(student_list, Student.class);
//        }
//
//        return studentList;
//    }


    @Override
    public List<Student> queryAll() {
        List<Student> studentList = null;
        String student_list = (String)redisTemplate.opsForValue().get("Student_List");
        // 判断redis中是否存在该值
        if(student_list == null || student_list.isEmpty()){
            synchronized (this){
                student_list = (String)redisTemplate.opsForValue().get("Student_List");
                if(student_list == null || student_list.isEmpty()){
                    System.out.println("-----------------在数据库中查看--------------------------");
                    // redis中没有该值
                    studentList = studentMapper.queryAll();
                    // 将该值存入redis
                    String jsonString = JSON.toJSONString(studentList);
                    redisTemplate.opsForValue().set("Student_List",jsonString);
                }else{
                    System.out.println("-----------------在redis中库中查看--------------------------");
                    studentList = JSONArray.parseArray(student_list, Student.class);
                }
            }
        }else{
            System.out.println("-----------------在redis中库中查看--------------------------");
            studentList = JSONArray.parseArray(student_list, Student.class);
        }

        return studentList;
    }



}